package top.kaoshanji.p3ch17b;

import org.apache.hc.core5.http.ContentType;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 代码清单-p3ch17b-292页a：被ServletContainerInitializer注册的Servlet
 * URL访问地址：/useful
 * @author kaoshanji
 * ^_^
 * create time 2025/4/9 17:03
 */
public class UsefulServlet extends HttpServlet {

    private static final long serialVersionUID = -6045338L;

    // 自己设定一个名字
    private String name = "UsefulServlet";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置响应对象的编码和内容格式
        resp.setContentType(ContentType.TEXT_HTML.toString());
        PrintWriter writer = resp.getWriter();

        writer.println("<html><head><title>First servlet" +
                "</title></head><body>" + name);
        writer.println("</body></head>");

    }

}
